<!--后台用户管理 -->
<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="dialogVisible = true">添加用户</el-button>
					<!-- <el-button type="primary" @click="handleAdd = true">添加角色</el-button> -->
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="List" highlight-current-row v-loading="loading" @selection-change="selsChange" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" align="center" label="ID" width="60">
			</el-table-column>
			<el-table-column prop="realName" align="center" label="用户名">
			</el-table-column>
			<el-table-column prop="mobile" align="center" label="手机号" >
			</el-table-column>
			<el-table-column prop="roleName" align="center" label="角色名称" >
			</el-table-column>
			<el-table-column prop="userState" align="center" label="状态" :formatter="formatSex">
			</el-table-column>
			<el-table-column prop="createTime" align="center" label="时间" >
			</el-table-column>
			<el-table-column label="操作" align="center" fixed="right" >
				<template slot-scope="scope">
					<el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					<el-button size="small" type="warning" @click="handlesetup(scope.$index,scope.row)">设置角色</el-button>
					<!-- <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
				</template>
			</el-table-column>
		</el-table>
		<!--批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination background layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!--角色添加界面-->
		<el-dialog title="添加管理员" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="姓名" prop="realName">
					<el-input v-model="addForm.realName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="mobile">
					<el-input v-model="addForm.mobile" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="pwd">
					<el-input v-model="addForm.pwd" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-radio-group v-model="addForm.status">
						<el-radio class="radio" :label="1">可用</el-radio>
						<el-radio class="radio" :label="0">不可用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary"  @click.native="addSubmit"  :loading="addLoading">确定</el-button>
			</span>
		</el-dialog>
		<!--设置权限界面-->
		<el-dialog :title="realName + ' 角色权限设置'" :visible.sync="setupFormVisible" width="50%" :before-close="handleClose">
			<el-form label-width="80px" :rules="setupdFormRules" ref="setupForm">
				<el-form-item label="角色选择" prop="status">
					<el-radio v-for="(value,index) in setupForm" :key='index' v-model="radio" class="radio" :label="value.roleid" >{{value.role_name}}</el-radio>
					<input type="text" :value="user_id" style="display: none;">
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="setupFormVisible = false">取消</el-button>
				<el-button type="primary" @click="setupSubmit" :loading="editLoading">确定</el-button>
			</div>
		</el-dialog>
		<!--编辑界面-->
		<el-dialog title="修改管理员" :visible.sync="editFormVisible" width="50%" :before-close="handleClose">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="姓名" prop="realName">
					<el-input v-model="editForm.realName" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机号" prop="mobile">
					<el-input v-model="editForm.mobile" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="pwd">
					<el-input v-model="editForm.pwd" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-radio-group v-model="editForm.status">
						<el-radio class="radio" :label="1">可用</el-radio>
						<el-radio class="radio" :label="0">不可用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">确定</el-button>
			</div>
		</el-dialog>
	</section>
</template>
<script>
	import util from '../../../common/js/util'
	import {
		getAdminList,
		addAdmin,
		editAdmin,
		delAdmin,
		RolesList,
		AuthorityAdmin,
		Refreshtoken
	} from '../../../api/api';

	export default {
		data() {
			return {
				radio: '',
				filters: {
					name: ''
				},
				loading: false,
				List: [],
				total: 0,
				page: 1,
				sels: [],//列表选中列
				listLoading: false,
				dialogVisible: false,
				editFormVisible:false,
				setupFormVisible:false,
				addLoading: false,
				editLoading: false,
				//新增界面数据
				addForm: {
					realName: '',
					mobile: '',
					pwd: '',
					status:-1
				},
				// 修改界面数据
				editForm: {
					realName: '',
					mobile: '',
					pwd: '',
					status:-1
				},
				setupForm:[],
				user_id:{},
				addFormRules: {
					realName: [{
						required: true,
						message: '请输入名称',
						trigger: 'blur'
					}],
					mobile: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur'
					}],
					pwd: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}],
					status: [{
						required: true,
						message: '请输入状态',
						trigger: 'blur'
					}]
				},
				editFormRules: {
					realName: [{
						required: true,
						message: '请输入名称',
						trigger: 'blur'
					}],
					mobile: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur'
					}],
					pwd: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}],
					status: [{
						required: true,
						message: '请输入状态',
						trigger: 'blur'
					}]
				},
				setupdFormRules:{
					status: [{
						required: true,
						message: '请选择角色',
						trigger: 'blur'
					}]
				},
				realName:'',
				timer: null
			}
		},
		mounted() {
			var Token = sessionStorage.getItem('Token');
			var timestamp=new Date().getTime();
			let para = {
				token: Token,
				curpage:1,
				pagesize:10,
				timestamp: timestamp
			};
			this.loading = true;
			getAdminList(para).then((res) => {
				var res = res.data;
				if (res.code != 0) {
					this.$message({
						message: res.message,
						type: 'error'
					});
				} else {
					this.List = res.data.list;
					this.loading = false;
				}
			});
			
			this.queryInfo();
			this.timer = setInterval(() => {
			   setTimeout(this.queryInfo, 0)
		    }, 600000)
		},
		methods: {
			// 设置定时器
			queryInfo() {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {token: Token,timestamp: timestamp};
				Refreshtoken(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.loading = false;
					}
				});
			},
			// 转换用户状态
			formatSex: function (row, column) {
				return row.userState == 1 ? '正常' : row.userState == 0 ? '禁用' : '未知';
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			cellStyle({row, column}){
				return 'text-align:center';
			},
			rowClass({row, column}){
				return 'text-align:center';
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
			},
			
			//获取用户列表
			getUsers: function() {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {
					token: Token,
					curpage:1,
					pagesize:10,
					timestamp: timestamp
				};
				this.loading = true;
				getAdminList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.total = res.data.total;
						this.List = res.data.list;
						this.loading = false;
					}
				});
			},
			// 添加角色
			handleClose(done) {
				this.$confirm('确认关闭？')
				    .then(_ => {
						done();
					})
					.catch(_ => {});
			},
			//添加角色提交
			addSubmit: function() {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp=new Date().getTime();
							let para =Object.assign({ token: Token, timestamp:timestamp,}, this.addForm);
							addAdmin(para).then((res) => {
								this.addLoading = false;
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.dialogVisible = false;
								this.getUsers();
							});
						});
					}
				});
			},
			// 修改角色
			handleEdit(index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp=new Date().getTime();
							let para = Object.assign({token: Token, timestamp:timestamp,}, this.editForm);
							editAdmin(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getUsers();
							});
						});
					}
				});
			},
			// 设置权限
			handlesetup(index,row){
				this.setupFormVisible = true;
				this.listLoading = true;
				this.user_id = row.id;
				this.realName = row.realName
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {token: Token, timestamp:timestamp };
				RolesList(para).then((res) => {
					this.listLoading = false;
					this.setupForm = res.data.data;
					this.loading = false;
				});
			},
			// 权限提交
			setupSubmit:function(){
				var rolesid = this.radio;
				var userId = this.user_id;
				if (rolesid != '') {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editLoading = true;
						var Token = sessionStorage.getItem('Token');
						var timestamp=new Date().getTime();
						let para = {token: Token, timestamp:timestamp,roleid: rolesid,userid:userId};
						AuthorityAdmin(para).then((res) => {
							this.editLoading = false;
							//NProgress.done();
							this.$message({
								message: '提交成功',
								type: 'success'
							});
							this.$refs['setupForm'].resetFields();
							this.setupFormVisible = false;
							this.getUsers();
						});
					});
				}else{
					this.$message({
							message: '请选择角色后再提交',
							type: 'error'
						});
				}
			},
			//单个/批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					var Token = sessionStorage.getItem('Token');
					var timestamp=new Date().getTime();
					let para = {token: Token, timestamp:timestamp, ids: ids };
					delAdmin(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getUsers();
					});
				}).catch(() => {
			
				});
			}
		},
		beforeDestroy() {
		  clearInterval(this.timer);        
		  this.timer = null;
		}
	};
</script>

<style scoped>
.el-button.is-disabled{
	color: #bfcbd9;
	    cursor: not-allowed;
	    background-image: none;
	    background-color: #eef1f6;
	    border-color: #d1dbe5;
}
.el-pagination {
    white-space: nowrap;
    padding: 2px 5px;
    color: #48576a;
}
</style>
